package com.kobako.model;

import com.alibaba.fastjson.JSON;
import com.kobako.entity.WebViewEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

/**
 * Created by kobako on 2017/3/17.
 * Just a game
 */
@Service(value = "chartDao")
public class ChartDao {
    Random rand = new Random();
    List<WebViewEntity> viewEntities = new ArrayList<>();

    public String getPageViewsJson(){
        String time = generateTime();
        fillViews(time);
        return JSON.toJSONString(viewEntities);
    }

    private void fillViews(String time){
        viewEntities.clear();
        for(int i=1;i<11;i++){
            int number = rand.nextInt(i*5)+1;
            WebViewEntity entity = new WebViewEntity();
            entity.setNowTime(time);
            entity.setTheValue(String .valueOf(number));
            viewEntities.add(entity);
        }
    }

    private String generateTime(){
        Calendar c = Calendar.getInstance();
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);

        String min = String .valueOf(minute);
        String sec = String .valueOf(second);

        if(minute<10){
            min = "0"+min;
        }
        if(second<10){
            sec = "0"+sec;
        }
        String time = min+ ":"+sec;
        return time;
    }

//    @org.junit.Test
//    public void testIt(){
//        ChartDao dao = new ChartDao();
//        System.out.println(dao.getPageViewsJson());
//        System.out.println(dao.viewEntities.size());
//    }
}
